/* Author: yangzeming
 * Date: 2013-12-10
 * Desc: 地图组件
 */
define("component/map", ["component/dialog"], function (require, exports, module) {

    var dialog = require("./dialog");

    var Map = _.Class({
        initial: function (options) {

            this.defaultDipTooltip = 'default';
            var callB = null;
            var self = this;
            var defaultOp = {
                showType: "map",
                mapOp: {
                    fillOpacity: 0.4,
                    fillColor: "d42e16",
                    strokeColor: "3320FF",
                    strokeOpacity: 0.8,
                    wrapCss: {
                        position: "absolute"
                    },
                    strokeWidth: 0,
                    tipClose: callB,
                    stroke: true,
                    isSelectable: true,
                    singleSelect: true,
                    mapKey: 'name',
                    listKey: 'name',
                    onShowToolTip: function (data) {
                        switch (data.key) {
                            case "haikou":
                                self.genericToolTip("HK", "海口市", data.toolTip, "#j_haikou_dibiao");
                                break;
                            case "dongfang":
                                self.genericToolTip("DF", "东方市", data.toolTip, "#j_dongfang_dibiao");
                                break;
                            case "qionghai":
                                self.genericToolTip("QH", "琼海市", data.toolTip, "#j_qionghai_dibiao");
                                break;
                            case "sanya":
                                self.genericToolTip("SY", "三亚市", data.toolTip, "#j_sanya_dibiao");
                                break;

                        }
                        //data.toolTip.html("hahahadfsdf");
                    },
                    onClick: function (e) {
                        //var newToolTip = defaultDipTooltip;

                        var isSelect = self.$con.mapster("get", e.key);

                        if (isSelect) {
                            self.$con.mapster("tooltip");
                        } else {
                            self.$con.mapster("tooltip", e.key);
                        }
                        /*if (e.key === 'haikou') {
                        var d = new dialog({
                        html: "海口",
                        title: "海口市详情"

                        });
                        }
                        if (e.key === 'dongfang') {
                        var d = new dialog({
                        html: "东方",
                        title: "东方市详情",
                        effect: {}

                        });
                        }
                        if (e.key === 'sanya') {
                        var d = new dialog({
                        html: "三亚",
                        title: "三亚市详情"

                        });
                        }
                        if (e.key === 'qionghai') {
                        var d = new dialog({
                        html: "琼海",
                        title: "琼海市详情"

                        });
                        }*/

                    },
                    showToolTip: true,
                    toolTipClose: [],  //"tooltip-click",mouseout

                    singleSelect: true,

                    areas: [

                        {
                            key: "qionghai",
                            fillColor: "46d300",

                            toolTip: this.defaultDipTooltip

                        },
                        {
                            key: "sanya",
                            fillColor: "46d300",
                            toolTip: this.defaultDipTooltip
                        },
                        {
                            key: "dongfang",
                            fillColor: "46d300",
                            toolTip: this.defaultDipTooltip
                        },
                        {
                            key: "asparagus",
                            strokeColor: "40c8a9"
                        },
                        {
                            key: "wanning",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "lingshui",
                            fillColor: "40c8a9"
                        }
				        ,
                        {
                            key: "wenchang",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "baoting",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "ledong",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "wuzhishan",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "qiongzhong",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "chanjiang",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "baisha",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "danzhou",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "lingao",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "tuchang",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "chengmai",
                            fillColor: "40c8a9"
                        },
                        {
                            key: "haikou",
                            fillColor: "46d300",
                            //selected: true,

                            toolTip: this.defaultDipTooltip
                        }
                    ]
                }

            };
            this.options = $.extend(true, {}, defaultOp, options);
            this.$con = $(options.con);
            this.$image = $(options.image);
            this.personTip = "hello";
            this.$dibiao = "#haikou";
            this.createMap();

            this.showIcon();
        },
        createMap: function () {
            var self = this;
            this.$con.mapster(this.options.mapOp);


        },
        genericToolTip: function (area, name, tooltip, dibiao) {
            // console.log(this.options.showType);
            if (this.options.showType == "map") {
                $.post("/home/home/ReturnJSONAreaCount", { Area: area }, function (result) {
                    if (+result.status == 1) {
                        var _html = '<ul>',
                            data = result.data;
                        _html += '<li style="color:#36FFA6;font-size:14px;font-weight:bold;margin-bottom:5px;">' + name + '</li>';
                        for (var i = 0, len = data.length; i < len; i++) {
                            _html += '<li style="line-height:20px;"><span>' + data[i]["Name"] + ':</span><span style="margin-left:10px;">' + data[i]["Count"] + '</span></li>';
                        }
                        if (data.length == 0) {
                            _html += "<li>暂无数据</li>";
                        }
                        _html += '</ul>';
                        tooltip.html(_html);
                        tooltip.show().position({
                            of: $(dibiao),
                            at: "left top",
                            my: (area == "DF" ? "right+110% top-100%" : "right top-100%")
                        }).hide().effect("explode", { "mode": "show" }, 500, function () {

                        });
                        //                    tooltip.css({ opacity: 0 }).show().position({
                        //                        of: $("#j_haikou_dibiao"),
                        //                        at: "left top",
                        //                        my: "right top-100%"
                        //                    }).hide().css("opacity", 0.9).effect("explode", { "mode": "show" }, 300, function () {

                        //                    });


                    } else {

                    }

                }, "json");
            } else {

                this.showPersonTip(tooltip, dibiao, area);
            }

        },
        showPersonTip: function (tooltip, dibiao, area) {
            tooltip.html(this.getPersonToolTip());
            //tooltip.css({ left: "636.5px", top: "58px" }).effect("explode", { "mode": "show" }, 500, function () {

            //});
            tooltip.show().position({
                of: $(dibiao),
                at: "left top",
                my: (area == "DF" ? "right+110% top-100%" : "right top-100%")
            }).hide().effect("explode", { "mode": "show" }, 500, function () {

            });
           // $(dibiao).hide().effect("shake", { "mode": "show" }, 1000, function () { });
        },
        toolTip: function (key) {
            this.$con.mapster('tooltip');
            this.$con.mapster('tooltip', key || "haikou");
        },
        setPersonToolTip: function (param) {
            this.personTip = param;
        },
        getPersonToolTip: function () {
            return this.personTip;
        },
        deSelect: function (key) {
            $('area').filter('[name="' + key + '"]').mapster('deselect');
        },
        selectArea: function (key) {
            var target = $('area').filter('[name="' + key + '"]');
            target.mapster('select');
            //target.mapster('tooltip');
            this.$con.mapster('tooltip');
            this.$con.mapster('tooltip', key);
            //console.log(this.$con.mapster('get_options'));
        },
        showToolTip: function (key) {
            var target = $('area').filter('[name="' + key + '"]');

            target.mapster('tooltip');

        },
        showIcon: function () {
            $(".j_dibiao").effect("clip", { "mode": "show" }, 800, function () {
            });

        }

    });
    module.exports = Map;


});